package cn.aiyuan.entity.base;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 服务器响应内容类
 *
 * @author 姜兆伟
 * @date 2019/7/16 12:02
 */
public class JsonData implements Serializable {

	private static final long serialVersionUID = 1L;

	/**
	 * 请求成功
	 */
	private static final Integer SUCCESS_CODE = 10000;

	/**
	 * 未知错误
	 */
	private static final Integer UNKNOWN_ERROR = 10001;

	/**
	 * 参数错误
	 */
	private static final Integer PARAMETER_ERROR = 10002;

	/**
	 * 未登录
	 */
	private static final Integer NO_LOGIN = 10003;

	/**
	 * 资源未找到
	 */
	private static final Integer NOT_FIND = 10004;

	/**
	 * 无权限
	 */
	private static final Integer UNAUTHORIZED = 10005;

	/**
	 * 请求失败
	 */
	private static final Integer FAIL_CODE = 10006;

	/**
	 * 自定义服务错误
	 */
	private static final Integer SERVICE_ERROR_CODE = 10007;

	/**
	 * 重复提交
	 */
	private static final Integer REPEAT_SUBMIT = 10008;

	@ApiModelProperty(value = "状态码")
	private Integer code;

	@ApiModelProperty(value = "数据")
	private Object data;

	@ApiModelProperty(value = "描述")
	private String msg;

	public JsonData() {
	}

	public JsonData(Integer code, Object data, String msg) {
		this.code = code;
		this.data = data;
		this.msg = msg;
	}

	public static JsonData ok() {
		return new JsonData(SUCCESS_CODE, null, "操作成功");
	}

	public static JsonData ok(Object object) {
		return new JsonData(SUCCESS_CODE, object, "操作成功");
	}

	public static JsonData ok(Object object, String msg) {
		return new JsonData(SUCCESS_CODE, object, msg);
	}

	public static JsonData okMsg(String msg) {
		return new JsonData(SUCCESS_CODE, null, msg);
	}

	/**
	 * 操作失败
	 *
	 * @param msg 错误信息
	 * @return
	 */
	public static JsonData fail(String msg) {
		return new JsonData(FAIL_CODE, null, msg);
	}

	/**
	 * 操作失败 默认返回 '操作失败'
	 *
	 * @return
	 */
	public static JsonData fail() {
		return new JsonData(FAIL_CODE, null, "操作失败");
	}

	/**
	 * 操作失败 默认返回 '操作失败'
	 *
	 * @param object 错误数据
	 * @return
	 */
	public static JsonData fail(Object object) {
		return new JsonData(FAIL_CODE, object, "操作失败");
	}

	/**
	 * 未知错误
	 *
	 * @return
	 */
	public static JsonData unknownError() {
		return new JsonData(UNKNOWN_ERROR, null, "服务器错误,请稍后再试!");
	}

	/**
	 * 参数错误
	 *
	 * @param msg 错误信息
	 * @return
	 */
	public static JsonData parameterError(String msg) {
		return new JsonData(PARAMETER_ERROR, null, msg);
	}

	/**
	 * 未登录
	 *
	 * @return
	 */
	public static JsonData noLogin() {
		return new JsonData(NO_LOGIN, null, "未登录");
	}

	/**
	 * 资源未找到
	 *
	 * @return
	 */
	public static JsonData notFind() {
		return new JsonData(NOT_FIND, null, "资源未找到");
	}

	/**
	 * 没有访问权限
	 *
	 * @return
	 */
	public static JsonData unauthorized() {
		return new JsonData(UNAUTHORIZED, null, "没有访问权限");
	}

	/**
	 * 服务错误
	 *
	 * @return
	 */
	public static JsonData serviceError(String msg) {
		return new JsonData(SERVICE_ERROR_CODE, null, msg);
	}

	/**
	 * 重复提交
	 *
	 * @return
	 */
	public static JsonData repeatSubmit() {
		return new JsonData(REPEAT_SUBMIT, null, "请勿重复提交");
	}

	/**
	 * 自定义返回数据
	 *
	 * @param obj
	 * @return
	 */
	public JsonData data(Object obj) {
		setData(obj);
		return this;
	}

	/**
	 * 自定义状态信息
	 *
	 * @param message
	 * @return
	 */
	public JsonData message(String message) {
		setMsg(message);
		return this;
	}

	/**
	 * 自定义状态码
	 *
	 * @param code
	 * @return
	 */
	public JsonData code(Integer code) {
		setCode(code);
		return this;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Override
	public String toString() {
		return "JsonData [code=" + code + ", data=" + data + ", msg=" + msg
				+ "]";
	}

}
